module.exports = function($scope, $modal, $modalInstance, $state, $resource, bean,toaster){

	console.log(bean);

	//推广图片
	$scope.imagearr = [];

	//推广文字数组
	$scope.textarr = [];

	//推广文字
	$scope.text = '';


	if(bean.material_pic){
		$scope.imagearr = bean.material_pic.split(',');
	}

  console.log(typeof(bean.material_words));
	if(bean.material_words){
		$scope.textarr = JSON.parse(bean.material_words);
	}


	//----------------- 多图 ------------------------------//
	$scope.setImage = function () {

		var para = $state.get('app.imageupload');
		//设置上传文件夹，以自己业务命名
		angular.extend(para, {
			resolve : {  
	            'dir' : function(){
	                return 'pic';
	            }
	        } 
		})

        var modalInstance = $modal.open(para);
        modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
        });  
        modalInstance.result.then(function(result) {  

        	console.log(result);
        	for(var i = 0; i < result.length; i++){
        		$scope.imagearr.push(result[i]);
        	}
        	

            //$scope.imagearr = JSON.stringify(result);
            // form.result['templete_lock_data_json'] = JSON.stringify(result.lock);
            // form.result['templete_check_data_json'] = result.unlock;

        }, function(reason) {  
            // click，点击取消，则会暑促cancel  
            $log.info('Modal dismissed at: ' + new Date());  
        }); 

	}
		
	$scope.imageDel = function(index){

		$scope.imagearr.splice(index, 1);
	};

	$scope.imageDown = function(index){
		$scope.imagearr[index + 1] = $scope.imagearr.splice(index, 1, $scope.imagearr[index + 1])[0];
	};

	$scope.imageUp = function(index){
		$scope.imagearr[index - 1] = $scope.imagearr.splice(index, 1, $scope.imagearr[index - 1])[0];
	};
	//----------------- 多图 ------------------------------//

  var dic = {
    'text' : ''
  };
	//---------- 推广文字 --------------------------//
	$scope.setText = function(){
    dic = {'text' : $scope.text};
    $scope.textarr.push(dic);
    console.log($scope.textarr);
    $scope.text = '';
    dic = '';

	}

	$scope.del_text = function(index){

		$scope.textarr.splice(index, 1);
	}

	$scope.textDown = function(index){
		$scope.textarr[index + 1] = $scope.textarr.splice(index, 1, $scope.textarr[index + 1])[0];
	}

	$scope.textUp = function(index){
		$scope.textarr[index - 1] = $scope.textarr.splice(index, 1, $scope.textarr[index - 1])[0];
	}
	//---------- 推广文字 --------------------------//


	$scope.gogo = function(){

		var image_txt = '';
		for(var i = 0, j = $scope.imagearr.length; i < j; i++){
			image_txt += $scope.imagearr[i] + (i == j - 1 ? '' : ',');
		}

		var text_txt = '';
		// for(var i = 0, j = $scope.textarr.length; i < j; i++){
		// 	text_txt += $scope.textarr[i] + (i == j - 1 ? '' : ',');
    // }
    text_txt = JSON.stringify($scope.textarr);
    console.log(text_txt);


		var para = {
			'id' : bean.id,
			'material_pic' : image_txt,
			'material_words' : text_txt,
		}

		console.log(para);

		$resource('/api/as/mc/shopproduct/updateMaterial', {}, {}).save(para, function (res) {
			console.log(res);

			if (res.errcode !== 0) {
				toaster.error({ title: '', body: res.errmsg });
				return;
			}
      toaster.success({ title: '', body: '添加素材成功' });
			$modalInstance.close();

		});

	}


	$scope.cancel = function () {
        $modalInstance.close();
    }

};
